import 'package:dio/dio.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';

import 'model/income_entity.dart';
import 'model/tsprofit_entity.dart';

class InCome extends StatefulWidget {
  int index;

  InCome(this.index);

  @override
  _InComeState createState() => _InComeState();
}

class _InComeState extends State<InCome>  with AutomaticKeepAliveClientMixin  {
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
  int currentPage = 1;
  IncomeEntity entity;
  MyPageState ps = MyPageState.LOADING;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return getContent();
  }

  getTop() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          0, ScreenUtil().setWidth(20), 0, ScreenUtil().setWidth(20)),
      child: Row(
        children: <Widget>[
          getItem("序号", 1),
          getItem("来源账号", 2),
          getItem("入账时间", 2),
          getItem("入账类型", 2),
          getItem("金额", 1),
        ],
      ),
    );
  }

  getItem(String s, int i) {
    return Expanded(
      flex: i,
      child: Container(
        alignment: Alignment.center,
        child: Text(
          s,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: getStyle(),
        ),
      ),
    );
  }

  getStyle() {
    return TextStyles.custom(26, Colours.appC2, Font.PingFangRegular);
  }

  void getData() {
    FormData formData = FormData.from({
      "type": widget.index,
      "page": currentPage,
      "size": 1000,
    });
    DioManager.getInstance().get(Api.USER_TSPROFIT, formData, (json) {
      entity = IncomeEntity.fromJson(json);
      if (null == entity) {
        ToastUtil.showShortErrToast(Api.ERR_NOTHING);
        NavigatorUtils.goBack(context);
        return;
      }

      if (entity.code != 200) {
        ToastUtil.showShortErrToast(entity.message);
        NavigatorUtils.goBack(context);
        return;
      }
      ps = MyPageState.HAVE_DATA;
      setState(() {});
    }, (err) {
      ToastUtil.showShortErrToast(Api.ERR_NOTHING);
      NavigatorUtils.goBack(context);
    });
  }

  Widget getContent() {
    if (ps == MyPageState.HAVE_DATA) {
      return Scaffold(
        backgroundColor: Colors.white,
        body: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(top: ScreenUtil().setWidth(20)),
              child: UiUtils.divider(height: 2),
            ),
            getTop(),
            UiUtils.divider(height: 1),
            Expanded(
              flex: 1,
              child: getList(),
            ),
          ],
        ),
      );
    }

    if (ps == MyPageState.LOADING) {
      return LoadingPage();
    }
    return Container();
  }

  getList() {
    List<Widget> list = [];

    if (entity.data.res != null && entity.data.res.length > 0) {
      for (int i = 0; i < entity.data.res.length; i++) {
        list.add(getItemWidget(entity.data.res[i], i));
      }
    }

    return ListView(
      children: list,
    );
  }

  Widget getItemWidget(IncomeDataRe item, int i) {
    return Container(
      margin: EdgeInsets.fromLTRB(0, ScreenUtil().setWidth(20), 0, 0),
      child: Row(
        children: <Widget>[
          getItem("${i + 1}", 1),
          getItem("${item.tel == null ? "---" : item.tel}", 2),
          getItem("${item.createdAt == null ? "" : item.createdAt}", 2),
          getItem("${item.memo == null ? "" : item.memo}", 2),
          getItem("${item.money == null ? "" : item.money}", 1),
        ],
      ),
    );
  }
}
